<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="/common/header.jsp"%>
<%@taglib prefix="code" tagdir="/WEB-INF/tags"%>
<html>
	<body class="easyui-layout list_body">
		<div class="list_body_north" data-options="region:'north'" border="false">
		 	<!-- 查询表单 -->
		 	<div  class="datagrid-toolbar" border="false"  >
					<span>角色名称:</span>
					<input type="text" id="role_name"  class="easyui-textbox" />
					<span style="margin-left:10px">角色类型:</span>
					<code:codeSelect name="role_tp" id="role_tp" code_type="ROLE_TYPE" showSelect="true"></code:codeSelect>
					<span style="margin-left:10px">所属机构:</span>
					<input type="text" id="ins_uuid"  class="easyui-textbox" />
				 	<a href="#" class="easyui-linkbutton btn" iconCls="icon-search" onclick="doSearch()">查询</a>
				 	<a href="#" class="easyui-linkbutton btn" iconCls="icon-reload" onclick="resetForm()">重置</a>
			</div>
			<!-- 自定义标签工具栏 -->
			<jiepos:buttonOperateTag menuCode="${menuCode}"></jiepos:buttonOperateTag>
		</div>
	    <div data-options="region:'center'" border="false" class="list_body_center">
			<!-- 表格 -->
		    <table id="roleinfTable" fit="true"></table>
		</div>
		<form method="post" id="authfrom" name="authfrom" action="<c:url value='/roleinf/saveMenuAuth.do'/>"></form>
	</body>
	<script type="text/javascript">
		var grid;
		$(function(){
			/*加载表格数据*/
			grid = $("#roleinfTable").jpgrid({
				url: "getRoleInfListJson.do",
				columns: [
					{title: "角色名称", field: "ROLE_NAME", width: 100},
					{title: "角色类型", field: "ROLE_TP_DESC", width: 80},
					{title: "所属机构", field: "INS_CN_NM", width: 220},
					{title: "角色描述", field: "ROLE_DESC"},
					{title: null, field: "ROLE_TP",hidden: true}
				]
			});
		});
		
		/*查询方法*/
		function doSearch(){ 
			grid.load({
				role_name: $('#role_name').val(),
				role_tp: $('#role_tp').val(),
				ins_uuid: $('#ins_uuid').val()
			});
		}
		
		/*新增方法*/
		function addRole(url){
			commonDetail(url, "save");
		}
		
		/*修改方法*/
		function modifyRole(url){
			var rowData = checkSelected("roleinfTable", true);
			if(rowData === false){
				return;
			}else if("${insLevel}" != 1 && rowData[0].ROLE_TP == 1){
				$.jpmessage.warning("不能修改公有角色信息");
			}else{
				commonDetail(url, "update", "roleinfTable");
			}
		}
		
		/*删除方法*/
		function deleteRole(url){
			var rowData = checkSelected("roleinfTable", true);
			if(rowData === false){
				return;
			}else if("${insLevel}" != 1 && rowData[0].ROLE_TP == 1){
				$.jpmessage.warning("不能删除公有角色");
			}else{
				commonDel(url, "delete", "roleinfTable");
			}
		}
		
		/*权限设置*/
		function authSet(url){
			var checkedItems = checkSelected("roleinfTable", true);
			if(!checkedItems){
				return;
			}
			// 获取角色代码
			var insLevel = "${insLevel}";
			var roleUuid = checkedItems[0].UUID;
			var roleType = checkedItems[0].ROLE_TP;
			var openIds = getMenuAuthByRoleId(roleUuid);
			var showCheckBox = true;
			/*非顶级机构，且是公有角色，则隐藏复选框*/
			if(insLevel != 1 && roleType == "1"){
				showCheckBox = false;
			}
			var options = {
				url: url + roleUuid + "&roleType=" + roleType + "&insLevel=" + insLevel,
				title: "选择菜单功能",
				chkboxType: {"Y": "p", "N": "s"},
				showCheckBox: showCheckBox,
				openIds: openIds,
				confirmCallback: function(nodes){
					callback(nodes, roleUuid, insLevel, roleType);
				}
			};
			showTree(options);
		}
		
		function callback(nodes, roleUuid, insLevel, roleType){
			if(insLevel != 1 && roleType == "1"){
				return;
			}
			/*用于存放角色功能关系*/
			var menuBtns = new Array();
			/*取菜单代码*/
			for(var i in nodes){
				var node = nodes[i];
				if((!node.children && !node.IS_BUTTON) || (node.children && node.children[0].IS_BUTTON)){// 无按钮的菜单或者只勾选菜单未勾选按钮
					menuBtns.push({role_uuid: roleUuid, menu_uuid: node.ID});
				}else if(node.IS_BUTTON){// 按钮
					if(menuBtns.length === 0){
						menuBtns.push({role_uuid: roleUuid, menu_uuid: node.PID, auth_buttons: node.ID});
						continue;
					}
					/*检查是否已经存在数组中*/
					var isPush = false;
					for(var j in menuBtns){
						var menuBtn = menuBtns[j];
						/*同级菜单，拼接按钮代码*/
						if(menuBtn.menu_uuid === node.PID){
							if(!menuBtns[j].auth_buttons){
								menuBtns[j].auth_buttons = node.ID;
							}else{
								menuBtns[j].auth_buttons = menuBtns[j].auth_buttons + "," + node.ID;
							}
							isPush = true;
							break;
						}
					}
					if(!isPush){
						menuBtns.push({role_uuid: roleUuid, menu_uuid: node.PID, auth_buttons: node.ID});
					}
				}
			}
			saveMenuAuth(roleUuid, menuBtns);
		}
		
		/*保存角色菜单按钮数据*/
		function saveMenuAuth(roleUuid, menuBtns){
			var input = "<input type='hidden' name='role_uuid' value='"+roleUuid+"'><input type='hidden' name='menuCode' value='${menuCode}'>";
			for(var i in menuBtns){
				var menuBtn = menuBtns[i];
				input += "<input type='hidden' name='menuAuth["+i+"].role_uuid' value='"+menuBtn.role_uuid+"'>";
				input += "<input type='hidden' name='menuAuth["+i+"].menu_uuid' value='"+menuBtn.menu_uuid+"'>";
				input += "<input type='hidden' name='menuAuth["+i+"].auth_buttons' value='"+(!menuBtn.auth_buttons ? "" : menuBtn.auth_buttons)+"'>";
			}
			$("#authfrom").html(input);
			$("#authfrom").submit();
		}
		
		/*根据角色代码查询权限*/
		function getMenuAuthByRoleId(roleUuid){
			var menuAuth;
			$.ajax({
				url: "getMenuAuthByRoleId.do",
				data: {roleUuid: roleUuid},
				async: false,
				success: function(result){
					menuAuth = result;
				}
			});
			return menuAuth;
		}
	</script>
</html>
